#include "lexer.h"

const char number = '8';

Token_stream::Token_stream(std::istream &is)
    : is(is), full(false), buffer{0}
{
}

Token Token_stream::get()
{
    if (full)
    {
        full = false;
        return buffer;
    }
    else
    {
        char ch;
        is >> ch;
        switch (ch)
        {
        case ';':
        case 'q':
        case '(':
        case ')':
        case '+':
        case '*':
        case '/':
        {
            return Token{ch};
        }
        case '.':
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
        {
            is.putback(ch);
            double val;
            is >> val;
            return Token{number, val};
        }
        default:
        {
            throw Lexer_error("Bad token");
        }
        }
    }
}

void Token_stream::putback(Token t)
{
    if (full)
    {
        throw Lexer_error("putback() into a full buffer");
    }
    buffer = t;
    full = true;
}

void Token_stream::ignore()
{
    full = false;
}